# 多无人机 每 2 台种一块 6x6 有间隔
# 16台无人机 一人一块3x6 每台再分一个助手 负责另外一半

from utils import *


def pick_start_pos(l):
    # '''每块南瓜田的左下角'''
    pumpkin_len = 6
    unit_size = pumpkin_len + 1
    pos = []
    for i in range(l):
        for j in range(l):
            if (i % unit_size) == 0 and (j % unit_size) == 0:
                pos.append((i,j))
    return pos


area_len_num = 4 # 一边有几个区块
l = area_len_num*(6+1)-1

set_world_size(l)

def do_subtask(t):
    x0,y0 = t
    pumpkin_len = 6
    
    def action(x,y):
        map.append((x,y))
        if get_ground_type() != Grounds.Soil:
            till()
        plant(Entities.Pumpkin)
        pourwater(.5)
    
    map = []
    snake_traverse(l,x0,y0,x0+pumpkin_len-1-3,y0+pumpkin_len-1,action)
    round = 0
    while 1:
        round += 1
        growing_map = []
        for pos in map:
            goto(l,pos[0],pos[1])
            if not can_harvest():
                plant(Entities.Pumpkin)
                growing_map.append(pos)
                if round > 3:
                    pourwater(.9)
                    if num_items(Items.Fertilizer) > 5 and len(map) == 1:
                        use_item(Items.Fertilizer)
                elif round > 1:
                    pourwater(.75)
                
        map = growing_map
        if len(map) == 0:
            # harvest()
            # round = 0
            break

def do_task(t):
    x0,y0 = t
    pumpkin_len = 6
    
    def action(x,y):
        map.append((x,y))
        if get_ground_type() != Grounds.Soil:
            till()
        plant(Entities.Pumpkin)
        pourwater(.5)
    
    while num_items(Items.Pumpkin) < 200000000:
        sub_drone = spawn_drone(create_task(do_subtask, (x0+3,y0)))
        map = []
        snake_traverse(l,x0,y0,x0+pumpkin_len-1-3,y0+pumpkin_len-1,action)
        round = 0
        while 1:
            round += 1
            growing_map = []
            for pos in map:
                goto(l,pos[0],pos[1])
                if not can_harvest():
                    plant(Entities.Pumpkin)
                    growing_map.append(pos)
                    if round > 3:
                        pourwater(.9)
                        if num_items(Items.Fertilizer) > 5 and len(map) == 1:
                            use_item(Items.Fertilizer)
                    elif round > 1:
                        pourwater(.75)
                    
            map = growing_map
            if len(map) == 0:
                while 1:
                    if has_finished(sub_drone):
                        harvest()
                        break
                round = 0
                break



all_start_pos = pick_start_pos(l)
for pos in all_start_pos[1:]:
    spawn_drone(create_task(do_task, pos))
while num_items(Items.Pumpkin) < 200000000:
    do_task(all_start_pos[0])
